package cn.jtfadmin.base.lang.config.excel;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import cn.jtfadmin.base.lang.config.ApplicationHolder;

import java.util.Objects;

/**
 * excel 参数
 * @author jtf
 * @since 0.0.1
 */
public class ExcelParam {

    /**
     * 获取缺省的导入参数
     * @return 导入参数
     */
    public static ImportParams getImportParams(){
        ImportParams importParams = new ImportParams();
        String[] beanNamesForType = ApplicationHolder.getApplicationContext().getBeanNamesForType(IExcelDictHandler.class);
        if(Objects.nonNull(beanNamesForType) && beanNamesForType.length>0){
            importParams.setDictHandler(ApplicationHolder.getApplicationContext().getBean(beanNamesForType[0],IExcelDictHandler.class ));
        }
        importParams.setNeedVerify(true);
        String[] excelVerifyHandlerNames = ApplicationHolder.getApplicationContext().getBeanNamesForType(IExcelVerifyHandler.class);
        if(Objects.nonNull(excelVerifyHandlerNames) && excelVerifyHandlerNames.length>0){
            importParams.setVerifyHandler(ApplicationHolder.getApplicationContext().getBean(excelVerifyHandlerNames[0],IExcelVerifyHandler.class ));
        }

        return importParams;
    }

    /**
     * 获取导出参数
     * @return
     */
    public static ExportParams getExportParams(){
        ExportParams exportParams = new ExportParams();
        exportParams.setType(ExcelType.XSSF);
        String[] beanNamesForType = ApplicationHolder.getApplicationContext().getBeanNamesForType(IExcelDictHandler.class);
        if(Objects.nonNull(beanNamesForType) && beanNamesForType.length>0){
            exportParams.setDictHandler(ApplicationHolder.getApplicationContext().getBean(beanNamesForType[0],IExcelDictHandler.class ));
        }
        return exportParams;
    }
}
